#include "AddEntry.h"
#include <iostream>
using namespace std;

AddEntry::AddEntry(char *dn,int auth_type,char *PassWord,char **optiuni,int nr_op,char *ldif_file):LDAP_Operation(dn,auth_type,optiuni,nr_op)
{
	result=new char[1000];
	if(result==0)
		throw new exception();
	strcpy(result,"ldapadd ");
	this->ldif_file = new char[strlen(ldif_file)+1];
	strcpy(this->ldif_file,ldif_file);

	this->LDAPpassWord = new char[strlen(PassWord)+1];
	strcpy(LDAPpassWord,PassWord);

}

AddEntry::AddEntry(char *dn,int auth_type,char *PassWord,char **optiuni,int nr_op,vector<Atribut<char *>> atribute):LDAP_Operation(dn,auth_type,optiuni,nr_op)
{
	result=new char[1000];
	if(result==0)
		throw new exception();
	strcpy(result,"ldapadd ");

	this->LDAPpassWord = new char[strlen(PassWord)+1];
	strcpy(LDAPpassWord,PassWord);

	this->atribute = atribute;
}


AddEntry::~AddEntry(void)
{
}


LDAP_Operation* AddEntry::operate()
{
	switch(this->auth_type)
	{
	case 0: strcat(result,"-x ");
		break;
	case 1: strcat(result," ");
		break;
	default: printf("\nExceptie:");
		throw new exception();
	}
	for(int i=0;i<nr_opt;i++)
	{
		strcat(result,option[i]);
		strcat(result," ");
	}

	strcat(result,DN);

	

	strcat(result," -w ");
	strcat(result,LDAPpassWord);
	strcat(result," -f ");

	_LDIF ldif_add(atribute);
	char ldif[100];
	strcpy(ldif,ldif_add.Da_LDIF());
	strcat(result,ldif);

	this->ldif_file = new char[strlen(ldif)+1];
	strcpy(ldif_file,ldif);

	cout<<"\nAddEntry=  "<<result<<"\n\n";
	LDAP_Operation *op=new AddEntry((*this));
	return op;
}